package org.jeecg.modules.middle.auditCenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.LimitSubmit;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.manage.entity.CaishuiBill;
import org.jeecg.modules.manage.entity.CaishuiTaskSettleList;
import org.jeecg.modules.middle.auditCenter.service.ICaishuiTaskSettleListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

 /**
 * @Description: 业务结算清单[中台]
 * @Author: yfkj
 * @Date:   2023-02-21
 * @Version: V1.0
 */
@Api(tags="业务结算清单[中台]")
@RestController
@RequestMapping("/auditCenter/caishuiTaskSettleList")
@Slf4j
public class CaishuiTaskSettleListController extends JeecgController<CaishuiTaskSettleList, ICaishuiTaskSettleListService> {

	@Autowired
	private ICaishuiTaskSettleListService caishuiTaskSettleListService;
	
	/**
	 * 分页列表查询
	 *
	 * @param caishuiTaskSettleList
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="业务结算清单[中台]-分页列表查询", notes="业务结算清单[中台]-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CaishuiTaskSettleList>> queryPageList(CaishuiTaskSettleList caishuiTaskSettleList,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CaishuiTaskSettleList> queryWrapper = QueryGenerator.initQueryWrapper(caishuiTaskSettleList, req.getParameterMap());
		Page<CaishuiTaskSettleList> page = new Page<CaishuiTaskSettleList>(pageNo, pageSize);
		IPage<CaishuiTaskSettleList> pageList = caishuiTaskSettleListService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 审核
	  * @param id
	  * @param status
	  * @return
	  */
	 @AutoLog(value = "业务结算清单[中台]-审核", terminal = 1)
	 @ApiOperation(value="业务结算清单[中台]-审核", notes="业务结算清单[中台]-审核")
	 @GetMapping(value = "/statusOne")
	 @LimitSubmit(key = "middle:taskSettleList:statusOne:%tId", spelKey = "#id")
	 public Result<?> statusOne(@RequestParam(name="id",required=true) String id,
								@RequestParam(name="status",required=true) int status,
								@RequestParam(name="rejectReason",required=false) String rejectReason) {
	 	if(status==3 && oConvertUtils.isEmpty(rejectReason)){
			return Result.error("请输入驳回理由！");
	 	}
		 CaishuiTaskSettleList settleList = caishuiTaskSettleListService.getById(id);
		 if(oConvertUtils.isEmpty(settleList)){
			 return Result.error("业务结算清单信息错误！");
		 }else if(settleList.getCheckStatus() != 1){
			 return Result.error("只有待审核的清单才能审核！");
		 }
		 settleList.setCheckStatus(status);
		 settleList.setCheckTime(new Date());
		 settleList.setCheckBy(TokenUtils.getLoginUser().getUsername());
		 settleList.setRejectReason(rejectReason);
		 caishuiTaskSettleListService.updateById(settleList);
		 return Result.OK("审核成功!");
	 }

}
